package java_thinking.unit_17.clone;

import lombok.Data;

/**
 * @Desc:   还可以用序列化来对对象进行深度克隆
 * @author: cww
 * @DateTime: 2019-04-27 14:22
 */
@Data
public class SchoolForDeep implements Cloneable{


    private String schoolName;
    private int stuNumbs;
    private StudentForDeep stu;

    /**
     * 在school的clone方法中将school中的stu对象手动clone一下。
     */
    @Override
    protected SchoolForDeep clone() throws CloneNotSupportedException {
        SchoolForDeep s = null;
        s = (SchoolForDeep)super.clone();
        s.stu = stu.clone();
        return s;
    }


    @Override
    public String toString() {
        return "School{" +
                "schoolName='" + schoolName + '\'' +
                ", stuNumbs=" + stuNumbs +
                ", stu=" + stu +
                '}';
    }

    /**
     * 深度克隆
     * @param args
     * @throws CloneNotSupportedException
     */
    public static void main(String[] args) throws CloneNotSupportedException {
        SchoolForDeep s1 = new SchoolForDeep();
        s1.setSchoolName("实验小学");
        s1.setStuNumbs(100);

        StudentForDeep stu1 = new StudentForDeep();
        stu1.setAge(29);
        stu1.setName("ww");
        stu1.setSex(new StringBuffer("男"));

        s1.setStu(stu1);
        System.out.println("s1: "+s1+" s1的hashcode:"+s1.hashCode()+"  s1中stu1的hashcode:"+s1.getStu().hashCode());
        SchoolForDeep s2 = s1.clone();
        System.out.println("s2: "+s2+" s2的hashcode:"+s2.hashCode()+"  s2中stu1的hashcode:"+s2.getStu().hashCode());
        System.out.println("s1 == s2 ? : " + (s1 == s2));
        System.out.println("s1 == s2 ? : " + (s1.equals(s2)));
        s2.setSchoolName("new School");
        s2.getStu().setAge(32);
        s2.getStu().setSex(new StringBuffer("fefeffefe"));
        System.out.println("s1: "+s1+" s1的hashcode:"+s1.hashCode()+"  s1中stu1的hashcode:"+s1.getStu().hashCode());
        System.out.println("s2: "+s2+" s2的hashcode:"+s2.hashCode()+"  s2中stu1的hashcode:"+s2.getStu().hashCode());



    }


}
